package com.halflife.netty_basics;

import com.halflife.netty_basics.common.ClinetInboundHandler;
import com.halflife.netty_basics.common.CommonOutboundHandler;
import com.halflife.netty_basics.model.TransferFile;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class Client {
    private Bootstrap bootstrap;
    private NioEventLoopGroup worker;
    public Client() {
        bootstrap = new Bootstrap();
        worker = new NioEventLoopGroup(1);
        bootstrap.group(worker).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new CommonOutboundHandler()).addLast(new ClinetInboundHandler());
            }
        });
        ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 9090).syncUninterruptibly();

        try {
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            worker.shutdownGracefully();
        }
    }

    public static void main(String[] args) {
        new Client();
    }

}
